// TypeScript file
class SoundBase extends egret.DisplayObjectContainer
{
    private url: string = "sound_bg_mp3"
    private sound: egret.Sound;
    private soundChannel: egret.SoundChannel;
    private postion: number = 0;
    private loop: number = 1;
    private status: number = SoundStatus.STOP;

    constructor(url?: string) 
    {
        super();
        if (url)
        {
            this.url = url;

        }
        this.sound = new egret.Sound();
        this.loadSound();
    }

    private loadSound(): void
    {
        let url: string = this.url;
        if (RES.getRes(url))
        {
            this.sound = RES.getRes(url);
        }
        else
        {
            let sound: egret.Sound = this.sound;
            sound.once(egret.Event.COMPLETE, this.loadComplete, this);
            sound.once(egret.IOErrorEvent.IO_ERROR, this.onLoadErr, this);
            sound.load(url);
        }
    }

    public play(): number
    {
        if (SoundStatus.LOADERR == this.status)
        {
            this.loadSound();
            return;
        }
        this.status = SoundStatus.PLAYING;
        if (this.soundChannel)
        {
            this.soundChannel.stop();
        }
        this.soundChannel = this.sound.play(this.postion, 1);
        this.soundChannel.once(egret.Event.SOUND_COMPLETE, this.looped, this);
        return this.status;
    }

    private looped(evt: egret.Event): void
    {
        console.log("looped");
        this.soundChannel = null;
        this.postion = 0;
        this.status = SoundStatus.STOP;
        let waring: string = "looped";
        if (this.loop >= 0)
        {
            this.dispatchEventWith(egret.Event.SOUND_COMPLETE, false, waring);
        }
        else
        {
            this.play();
        }
    }
    private loadComplete(evt: egret.Event): void
    {
        this.status = 3;
        let waring: string = "SoundBase.loadComplete";
        egret.log(waring);
        this.sound.removeEventListener(egret.IOErrorEvent.IO_ERROR, this.onLoadErr, this);
        this.dispatchEventWith(egret.Event.COMPLETE, false, waring);
    }

    private onLoadErr(evt: egret.IOErrorEvent): void
    {
        this.status = SoundStatus.LOADERR;
        let waring: string = "SoundBase.onLoadErr "+ this.url;
        egret.log(waring);
        

        this.sound.removeEventListener(egret.Event.COMPLETE, this.loadComplete, this);
        this.dispatchEventWith(egret.IOErrorEvent.IO_ERROR, false, waring);
    }

    public setURL(url: string): void
    {
        console.log("xxxxxxxxxxxxxxxxxxxxxx:soundbase::seturl:"+url)
        this.url = url;
        this.loadSound();
    }

    public getStatus(): number
    {
        return this.status;
    }

    public setVolume(volume: number): void
    {
        console.log(this.status);
        if (SoundStatus.PLAYING == this.status)
        {
            this.soundChannel.volume = volume / 100;
        }
    }

    public showPosition(): number
    {
        if (SoundStatus.PLAYING === this.status)
        {
            this.postion = this.soundChannel.position;
        }

        return this.postion;
    }

    public setLoop(loop: number): number
    {
        this.loop = loop;
        return loop;
    }

    public pause(): number
    {
        let temp = this.status;
        if (SoundStatus.PLAYING === temp)
        {
            let channel: egret.SoundChannel = this.soundChannel;
            this.postion = channel.position;
            channel.stop();
            this.status = SoundStatus.PAUSE;
        }
        egret.log(this.postion);
        return temp;
    }

    public resume(): number
    {
        let temp = this.status;
        if (SoundStatus.PAUSE === temp)
        {
            this.play();
        }
        egret.log(this.postion);
        return temp;
    }

}